package com.zlop.abc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 基于一个ReentrantLock和三个conditon实现连续打印abcabc...
 *
 */
public class RcSyncPrinter { 

	public static void main(String[] args) throws InterruptedException {
		ReentrantLock reentrantLock = new ReentrantLock();
		
		Condition conditionA = reentrantLock.newCondition();
		Condition conditionB = reentrantLock.newCondition();
		Condition conditionC = reentrantLock.newCondition();
		
		Printer printerA = new Printer(reentrantLock, conditionA, conditionB, 'A');
		Printer printerB = new Printer(reentrantLock, conditionB, conditionC, 'B');
		Printer printerC = new Printer(reentrantLock, conditionC, conditionA, 'C');
		
		Thread threadA = new Thread(printerA);
		Thread threadB = new Thread(printerB);
		Thread threadC = new Thread(printerC);
		
		threadA.start();
		Thread.sleep(100);
		threadB.start();
		Thread.sleep(100);
		threadC.start();
	}
	
}
